package com.dzzw.Servlet.UserServlet;

import com.dzzw.Dao.UserDao;
import com.dzzw.Model.User;
import com.dzzw.Service.UserService;
import com.dzzw.utils.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/View/FindAllUserServlet")
public class FindAllUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final UserDao userDao= new UserDao();
    public FindAllUserServlet() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        // 获取当前页数参数
        String pageIndex = request.getParameter("pageIndex");
        // 获取页面大小参数（每页显示的数量）
        String pageSize = request.getParameter("pageSize");
        int index = 1;// 设置首页为1
        int size = 1;// 设置页面大小
        try {
            if (pageIndex == null) {
                index = 1;
            } else {
                index = Integer.parseInt(pageIndex);
                size = Integer.parseInt(pageSize);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        UserService userService = new UserService();
        Page page=userService.FindAllUser(index,size);


       // 尾页填充空白行(为了视觉美观)
        List<User> dataList = page.getDataList();
        if (dataList.size() < page.getPageSize()) {
            for (int i = dataList.size(); i < page.getPageSize(); i++)
                dataList.add(null);
        }
        page.setDataList(dataList);

        // 将业务层处理后的分页对象存放至request作用域中
        request.setAttribute("page", page);
        request.getRequestDispatcher("/View/FindAllUser.jsp").forward(request, response);
    }
}
